/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controladores;

import controladores.exceptions.IllegalOrphanException;
import controladores.exceptions.NonexistentEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import modelo.Empleado;
import modelo.Banco;
import modelo.Libranza;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import modelo.Deuda;

/**
 *
 * @author Diana
 */
public class DeudaJpaController implements Serializable {

    public DeudaJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Deuda deuda) {
        if (deuda.getLibranzaList() == null) {
            deuda.setLibranzaList(new ArrayList<Libranza>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Empleado empleadoIdempleado = deuda.getEmpleadoIdempleado();
            if (empleadoIdempleado != null) {
                empleadoIdempleado = em.getReference(empleadoIdempleado.getClass(), empleadoIdempleado.getIdempleado());
                deuda.setEmpleadoIdempleado(empleadoIdempleado);
            }
            Banco bancoIdbanco = deuda.getBancoIdbanco();
            if (bancoIdbanco != null) {
                bancoIdbanco = em.getReference(bancoIdbanco.getClass(), bancoIdbanco.getIdbanco());
                deuda.setBancoIdbanco(bancoIdbanco);
            }
            List<Libranza> attachedLibranzaList = new ArrayList<Libranza>();
            for (Libranza libranzaListLibranzaToAttach : deuda.getLibranzaList()) {
                libranzaListLibranzaToAttach = em.getReference(libranzaListLibranzaToAttach.getClass(), libranzaListLibranzaToAttach.getIdlibranza());
                attachedLibranzaList.add(libranzaListLibranzaToAttach);
            }
            deuda.setLibranzaList(attachedLibranzaList);
            em.persist(deuda);
            if (empleadoIdempleado != null) {
                empleadoIdempleado.getDeudaList().add(deuda);
                empleadoIdempleado = em.merge(empleadoIdempleado);
            }
            if (bancoIdbanco != null) {
                bancoIdbanco.getDeudaList().add(deuda);
                bancoIdbanco = em.merge(bancoIdbanco);
            }
            for (Libranza libranzaListLibranza : deuda.getLibranzaList()) {
                Deuda oldDeudaIddeudaOfLibranzaListLibranza = libranzaListLibranza.getDeudaIddeuda();
                libranzaListLibranza.setDeudaIddeuda(deuda);
                libranzaListLibranza = em.merge(libranzaListLibranza);
                if (oldDeudaIddeudaOfLibranzaListLibranza != null) {
                    oldDeudaIddeudaOfLibranzaListLibranza.getLibranzaList().remove(libranzaListLibranza);
                    oldDeudaIddeudaOfLibranzaListLibranza = em.merge(oldDeudaIddeudaOfLibranzaListLibranza);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Deuda deuda) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Deuda persistentDeuda = em.find(Deuda.class, deuda.getIddeuda());
            Empleado empleadoIdempleadoOld = persistentDeuda.getEmpleadoIdempleado();
            Empleado empleadoIdempleadoNew = deuda.getEmpleadoIdempleado();
            Banco bancoIdbancoOld = persistentDeuda.getBancoIdbanco();
            Banco bancoIdbancoNew = deuda.getBancoIdbanco();
            List<Libranza> libranzaListOld = persistentDeuda.getLibranzaList();
            List<Libranza> libranzaListNew = deuda.getLibranzaList();
            List<String> illegalOrphanMessages = null;
            for (Libranza libranzaListOldLibranza : libranzaListOld) {
                if (!libranzaListNew.contains(libranzaListOldLibranza)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Libranza " + libranzaListOldLibranza + " since its deudaIddeuda field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (empleadoIdempleadoNew != null) {
                empleadoIdempleadoNew = em.getReference(empleadoIdempleadoNew.getClass(), empleadoIdempleadoNew.getIdempleado());
                deuda.setEmpleadoIdempleado(empleadoIdempleadoNew);
            }
            if (bancoIdbancoNew != null) {
                bancoIdbancoNew = em.getReference(bancoIdbancoNew.getClass(), bancoIdbancoNew.getIdbanco());
                deuda.setBancoIdbanco(bancoIdbancoNew);
            }
            List<Libranza> attachedLibranzaListNew = new ArrayList<Libranza>();
            for (Libranza libranzaListNewLibranzaToAttach : libranzaListNew) {
                libranzaListNewLibranzaToAttach = em.getReference(libranzaListNewLibranzaToAttach.getClass(), libranzaListNewLibranzaToAttach.getIdlibranza());
                attachedLibranzaListNew.add(libranzaListNewLibranzaToAttach);
            }
            libranzaListNew = attachedLibranzaListNew;
            deuda.setLibranzaList(libranzaListNew);
            deuda = em.merge(deuda);
            if (empleadoIdempleadoOld != null && !empleadoIdempleadoOld.equals(empleadoIdempleadoNew)) {
                empleadoIdempleadoOld.getDeudaList().remove(deuda);
                empleadoIdempleadoOld = em.merge(empleadoIdempleadoOld);
            }
            if (empleadoIdempleadoNew != null && !empleadoIdempleadoNew.equals(empleadoIdempleadoOld)) {
                empleadoIdempleadoNew.getDeudaList().add(deuda);
                empleadoIdempleadoNew = em.merge(empleadoIdempleadoNew);
            }
            if (bancoIdbancoOld != null && !bancoIdbancoOld.equals(bancoIdbancoNew)) {
                bancoIdbancoOld.getDeudaList().remove(deuda);
                bancoIdbancoOld = em.merge(bancoIdbancoOld);
            }
            if (bancoIdbancoNew != null && !bancoIdbancoNew.equals(bancoIdbancoOld)) {
                bancoIdbancoNew.getDeudaList().add(deuda);
                bancoIdbancoNew = em.merge(bancoIdbancoNew);
            }
            for (Libranza libranzaListNewLibranza : libranzaListNew) {
                if (!libranzaListOld.contains(libranzaListNewLibranza)) {
                    Deuda oldDeudaIddeudaOfLibranzaListNewLibranza = libranzaListNewLibranza.getDeudaIddeuda();
                    libranzaListNewLibranza.setDeudaIddeuda(deuda);
                    libranzaListNewLibranza = em.merge(libranzaListNewLibranza);
                    if (oldDeudaIddeudaOfLibranzaListNewLibranza != null && !oldDeudaIddeudaOfLibranzaListNewLibranza.equals(deuda)) {
                        oldDeudaIddeudaOfLibranzaListNewLibranza.getLibranzaList().remove(libranzaListNewLibranza);
                        oldDeudaIddeudaOfLibranzaListNewLibranza = em.merge(oldDeudaIddeudaOfLibranzaListNewLibranza);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = deuda.getIddeuda();
                if (findDeuda(id) == null) {
                    throw new NonexistentEntityException("The deuda with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Deuda deuda;
            try {
                deuda = em.getReference(Deuda.class, id);
                deuda.getIddeuda();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The deuda with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<Libranza> libranzaListOrphanCheck = deuda.getLibranzaList();
            for (Libranza libranzaListOrphanCheckLibranza : libranzaListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Deuda (" + deuda + ") cannot be destroyed since the Libranza " + libranzaListOrphanCheckLibranza + " in its libranzaList field has a non-nullable deudaIddeuda field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Empleado empleadoIdempleado = deuda.getEmpleadoIdempleado();
            if (empleadoIdempleado != null) {
                empleadoIdempleado.getDeudaList().remove(deuda);
                empleadoIdempleado = em.merge(empleadoIdempleado);
            }
            Banco bancoIdbanco = deuda.getBancoIdbanco();
            if (bancoIdbanco != null) {
                bancoIdbanco.getDeudaList().remove(deuda);
                bancoIdbanco = em.merge(bancoIdbanco);
            }
            em.remove(deuda);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Deuda> findDeudaEntities() {
        return findDeudaEntities(true, -1, -1);
    }

    public List<Deuda> findDeudaEntities(int maxResults, int firstResult) {
        return findDeudaEntities(false, maxResults, firstResult);
    }

    private List<Deuda> findDeudaEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Deuda.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Deuda findDeuda(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Deuda.class, id);
        } finally {
            em.close();
        }
    }

    public int getDeudaCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Deuda> rt = cq.from(Deuda.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
